package com.zhiyou.servlet;

import com.zhiyou.model.Lessee;
import com.zhiyou.model.Rent;
import com.zhiyou.service.RentService;
import com.zhiyou.service.impl.RentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class RentUpdateServlet extends HttpServlet {
    private RentService rS = new RentServiceImpl();
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("更新成功");
        Rent rent=null;
        String rid = req.getParameter("rid");
        String rhid = req.getParameter("rhid");
        String rlid = req.getParameter("rlid");
        String rprice = req.getParameter("rprice");
        String rpayTime = req.getParameter("rpayTime");

        if (rid!=""&&rhid!=""&&rlid!=""&&rprice!=""&&rpayTime!=""){
            rent = new Rent();//创建一个rent对象来封装数据
            rent.setRid(Integer.parseInt(rid));
            rent.setRhid(Integer.parseInt(rhid));
            rent.setRlid(Integer.parseInt(rlid));
            rent.setRprice(Double.parseDouble(rprice));
            try {
                rent.setRpayTime(new SimpleDateFormat("yy-MM-dd").parse(rpayTime));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            rS.updateNew(rent);
        }
        resp.sendRedirect(req.getContextPath()+"/user/rent?pageNo="+req.getParameter("pageNo"));





    }


    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("更新执行");
        String idStr = req.getParameter("rid");
        int rid=Integer.parseInt(idStr);
        //根据id查房租信息
        Rent rent = rS.selectById(rid);
        //将查询到的房租信息放入session域中
        req.getSession().setAttribute("rent",rent);
        //接受参数判断是更新还是详情
        String detail = req.getParameter("detail");
        if (detail!=null){
            resp.sendRedirect(req.getContextPath()+"/view/rent/detail.jsp");
        }else{
            resp.sendRedirect(req.getContextPath()+"/view/rent/edit.jsp");
        }

    }
}
